package com.zhiyan.security.account.webservice.rest;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zhiyan.common.util.model.DataModel;
import com.zhiyan.common.util.model.Page;
import com.zhiyan.common.web.SecurityContextHolder;
import com.zhiyan.security.account.domain.entity.Account;
import com.zhiyan.security.account.service.AccountService;

@RestController
@RequestMapping(value = "/security/account/rest")
public class AccountRestController {
	private static Logger logger = Logger.getLogger(AccountRestController.class);
	
	@Autowired
	private AccountService accountService;
	
	/**
	 * 保存用户信息
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save", method=RequestMethod.POST)
	public DataModel save(HttpServletRequest request, @RequestBody Account account){
		DataModel dataModel = new DataModel();
		try {
			if (account.getId()==null || account.getId().length()<=0) {	//新增
				accountService.add(account);
			}else{	//更新
				accountService.update(account);
			}
			dataModel.setSucc();
		} catch (Exception e) {
			dataModel.setFail();
			logger.error(e);
		}
		return dataModel;
	}
	
	/**
	 * 分页查询
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/queryPage", method=RequestMethod.POST)
	public DataModel queryPage(HttpServletRequest request, Integer pageNo, Integer pageSize){
		DataModel dataModel = new DataModel();
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			Page page = new Page(pageNo,pageSize);
			List<Account> accountList = accountService.queryPage(page, map);
			dataModel.setPageData(page, accountList);
		} catch (Exception e) {
			dataModel.setFail();
			logger.error(e);
		}
		return dataModel;
	}
	
	/**
	 * 分页查询
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/query", method=RequestMethod.POST)
	public DataModel query(HttpServletRequest request){
		DataModel dataModel = new DataModel();
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			Account account = SecurityContextHolder.getUser();
			if (account != null) {
				List<String> id = Arrays.asList(account.getId().split(","));
				map.put("id", id);
			}
			List<Account> accountList = accountService.query(map);
			dataModel.setData(accountList);
		} catch (Exception e) {
			dataModel.setFail();
			logger.error(e);
		}
		return dataModel;
	}
	
	/**
	 * 获取登录信息
	 * @return
	 */
	@RequestMapping(value="/getLoginInfo", method=RequestMethod.GET)
	public DataModel getLoginInfo(){
		DataModel dataModel = new DataModel();
		try {
			Account account = SecurityContextHolder.getUser();
			if (account!=null) {
				dataModel.setSucc();
				dataModel.setContent(account);
			}
		} catch (Exception e) {
			dataModel.setFail();
			logger.error(e);
		}
		return dataModel;
	}

}
